package com.ifgi.sitcom.campusmapperNDrawer.activities;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;




import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.plus.PlusClient;
import com.ifgi.sitcom.campusmapperNDrawer.R;

//this class held connections to API GooglePlus
public class BaseActivity extends SherlockFragmentActivity implements PlusClient.ConnectionCallbacks, 
PlusClient.OnConnectionFailedListener,
PlusClient.OnAccessRevokedListener,OnClickListener{

	private PlusClient mPlusClient;
	private static final int DIALOG_GET_GOOGLE_PLAY_SERVICES = 1;
    private static final int REQUEST_CODE_SIGN_IN = 1;
    private static final int REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES = 2;
    private ConnectionResult mConnectionResult;
	    
	@Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	      
	        mPlusClient = new PlusClient.Builder(
	                this, this, this)
	                    .setScopes(Scopes.PROFILE)
	                    .build();
	       
	        mPlusClient.connect();
	        
	        }
	
	 @Override
	    public void onStart() {
	        super.onStart();
	        Log.v("onStart","onStart");
	      
	        mPlusClient.connect();
	        
	    }

	    @Override
	    public void onStop() {
	        mPlusClient.disconnect();
	        Log.v("onStop","onStop");
	        super.onStop();
	    }

	    @Override
	    public void onResume() {
	       // mPlusClient.disconnect();
	        Log.v("onResume","onResume");
	        super.onResume();
	    }
	    
	    @Override
	    public void onRestart() {
	       // mPlusClient.disconnect();
	        Log.v("onRestart","onRestart");
	        super.onRestart();
	    }
	    
	    @Override
	    public void onPause() {
	       // mPlusClient.disconnect();
	        Log.v("onPause","onPause");
	        super.onPause();
	    }
	    
	@Override
	public void onAccessRevoked(ConnectionResult status) {
		// TODO Auto-generated method stub
		if (status.isSuccess()) {
			Intent intent=new Intent(this,SignInActivity.class);
			startActivity(intent);
			finish();
        } else {
          
            mPlusClient.disconnect();
           
        }
       
        Log.v("onrevoke","onrevoke "+this.getClass().toString());
	}

	@Override
	public void onConnectionFailed(ConnectionResult result) {
		// TODO Auto-generated method stub
		setmConnectionResult(result);
 		Log.v("onconnectFailed","onconnectFailed"+this.getClass().toString());
	}

	@Override
	public void onConnected(Bundle arg0) {
		// TODO Auto-generated method stub
	    Log.v("onConnected","onConnected"+this.getClass().toString());
	}

	public PlusClient getmPlusClient() {
		return mPlusClient;
	}

	public void setmPlusClient(PlusClient mPlusClient) {
		this.mPlusClient = mPlusClient;
	}

	@Override
	public void onDisconnected() {
		// TODO Auto-generated method stub
		 mPlusClient.connect();
	     Log.v("ondisconnected","ondisconnected"+this.getClass().toString());
	}
	

	public ConnectionResult getmConnectionResult() {
		return mConnectionResult;
	}

	public void setmConnectionResult(ConnectionResult mConnectionResult) {
		this.mConnectionResult = mConnectionResult;
	}

	 @Override
	    public void onActivityResult(int requestCode, int resultCode, Intent data) {
	        if (requestCode == REQUEST_CODE_SIGN_IN
	                || requestCode == REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES) {
	        	Log.v("5","5");
	            if (resultCode == RESULT_OK && !mPlusClient.isConnected()
	                    && !mPlusClient.isConnecting()) {
	                // This time, connect should succeed.
	            	Log.v("r5","5");
	                mPlusClient.connect();
	            }
	        } 
	        else {
	        	 Log.v("onactivit","onactivit");
	        	super.onActivityResult(requestCode, resultCode, data);
	        }
	        Log.v("onResult","onResult");
	        
	    }
	 
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
					if(v.getId()==R.id.sign_in_button)
						
					{
						int available = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
						Log.v("Available",String.valueOf(available));
						Log.v("Success",String.valueOf(ConnectionResult.SUCCESS));
		                if (available != ConnectionResult.SUCCESS) {
		                	
		                    showDialog(DIALOG_GET_GOOGLE_PLAY_SERVICES);
		                    return;
		                }

		                try {
		                	Log.v("33","33");
		                    mConnectionResult.startResolutionForResult(this, REQUEST_CODE_SIGN_IN);
		                } catch (IntentSender.SendIntentException e) {
		                    // Fetch a new result to start.
		                	Log.v("444","ff");
		                    mPlusClient.connect();
		                }
				}
					else onClick(v);
	}
	
	@Override
    protected Dialog onCreateDialog(int id) {
        if (id != DIALOG_GET_GOOGLE_PLAY_SERVICES) {
        	 Log.v("1", "1");
            return super.onCreateDialog(id); 
        }

        int available = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (available == ConnectionResult.SUCCESS) {
        	 Log.v("2", "2");
            return null;
        }
        if (GooglePlayServicesUtil.isUserRecoverableError(available)) {
        	 Log.v("3", "3");
            return GooglePlayServicesUtil.getErrorDialog(
                    available, this, REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES);
        }
        Log.v("4", "4");
        return new AlertDialog.Builder(this)
                .setMessage(R.string.plus_generic_error)
                .setCancelable(true)
                .create();
    }


}
